//
//  LoginOrRegisterViewController.m
//  MagicFang
//
//  Created by 华谕 on 2017/12/1.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "LoginOrRegisterViewController.h"

static int seconds = 60;//计时60s
@interface LoginOrRegisterViewController ()<UITextFieldDelegate>
{
    UIImageView *backImgView;
    UIView *theLineView;
    UITextField *loginPhoneField;
    UITextField *loginpasswField;
    UITextField *registpasswField1;
    UITextField *codeField;
    UIView *loginView;
    UIView *regisView;
    UIButton *codeBtn;
    NSTimer *timer;
    
    NSString *agreementStr;
    UIButton *agreement;
    BOOL isSelect;
}
@property (nonatomic, strong)UIButton *leftBtn;
@property (nonatomic, strong)UIButton *rightBtn;
@property (nonatomic, assign)NSInteger clickType;
@property (nonatomic, strong) UIView *coverView;
@property (nonatomic, strong) UIView *tipView;
@end

@implementation LoginOrRegisterViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jianpanhuishou:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    //    [timer invalidate];
    [self renewTimer];
    timer = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    isSelect = YES;
    _clickType = 0;
    [self requestRegistAgreement];
    [self creatUI];
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)creatUI
{
    self.view.backgroundColor = BacksColor;
    backImgView = [ZCControl createImageViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) ImageName:@"mf"];
    backImgView.contentMode = UIViewContentModeScaleAspectFill;
    backImgView.layer.masksToBounds = YES;
    [self.view addSubview:backImgView];
    UIButton *backBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth ,STA_NAV_HEIGHT-34, 20*displaywidth, 30) ImageName:@"fanhui" Target:self Action:@selector(backAction) Title:nil Font:15.0];
    backBtn.backgroundColor = [UIColor clearColor];
    [backImgView addSubview:backBtn];
    
    self.leftBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth ,260*displaywidth, SCREEN_WIDTH/2-30*displaywidth, 30*displaywidth) ImageName:nil Target:self Action:@selector(loginAction) Title:@"登录" Font:16.0];
    [_leftBtn setTitleColor:RGB(129, 148, 196) forState:UIControlStateNormal];
    _leftBtn.backgroundColor = [UIColor clearColor];
    [backImgView addSubview:_leftBtn];
    self.rightBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2+15*displaywidth ,260*displaywidth, SCREEN_WIDTH/2-30*displaywidth, 30*displaywidth) ImageName:nil Target:self Action:@selector(regisAction) Title:@"注册" Font:16.0];
    [_rightBtn setTitleColor:RGB(77, 77, 77) forState:UIControlStateNormal];
    _rightBtn.backgroundColor = [UIColor clearColor];
    [backImgView addSubview:_rightBtn];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 300*displaywidth, SCREEN_WIDTH, 2*displaywidth)];
    lineView.backgroundColor = LinesColor;
    [backImgView addSubview:lineView];
    theLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 300*displaywidth, SCREEN_WIDTH/2, 2*displaywidth)];
    theLineView.backgroundColor = ThemeColor;
    [backImgView addSubview:theLineView];
    
    NSArray *imgAry = @[@"shouji",@"mima"];
    NSArray *pladerAry = @[@"请输入手机号",@"请输入密码"];
    for (int i = 0; i < imgAry.count; i ++)
    {
        UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(35*displaywidth, 60*displaywidth*i+325*displaywidth, 30*displaywidth, 30*displaywidth) ImageName:imgAry[i]];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [backImgView addSubview:imgView];
        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(70*displaywidth, 60*displaywidth*i+325*displaywidth, SCREEN_WIDTH-135*displaywidth, 40*displaywidth) placeholder:pladerAry[i] passWord:NO leftImageView:nil rightImageView:nil Font:14.0];
        [textfield setValue:RGB(179, 179, 179) forKeyPath:@"_placeholderLabel.textColor"];
        textfield.textColor = RGB(77, 77, 77);
        textfield.delegate = self;
        [backImgView addSubview:textfield];
        UIView *lineViewa = [[UIView alloc] initWithFrame:CGRectMake(35*displaywidth, 369*displaywidth+60*displaywidth*i, SCREEN_WIDTH-70*displaywidth, 1*displaywidth)];
        lineViewa.backgroundColor = ThemeColor;
        [backImgView addSubview:lineViewa];
        if (i == 0)
        {
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            loginPhoneField = textfield;
        }
        else
        {
            textfield.secureTextEntry = YES;
            loginpasswField = textfield;
            UIButton *seeBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-60*displaywidth, 390*displaywidth, 25*displaywidth, 20*displaywidth) ImageName:@"biyan" Target:self Action:@selector(seePassAction:) Title:nil Font:12.0];
            seeBtn.backgroundColor = [UIColor clearColor];
            [backImgView addSubview:seeBtn];
        }
        
    }
    
    loginView = [[UIView alloc] initWithFrame:CGRectMake(0, 430*displaywidth, SCREEN_WIDTH, 150*displaywidth)];
    loginView.backgroundColor = [UIColor clearColor];
    [backImgView addSubview:loginView];
    
    UIButton *forgotBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-105*displaywidth, 10*displaywidth, 70*displaywidth, 20*displaywidth) ImageName:nil Target:self Action:@selector(FindPassword) Title:@"忘记密码" Font:13.0];
    [forgotBtn setTitleColor:RGB(153, 153, 153) forState:UIControlStateNormal];
    forgotBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    forgotBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [loginView addSubview:forgotBtn];
    
    UIButton *loginBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth , 60*displaywidth, SCREEN_WIDTH-120*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(loginBtnBtnAction) Title:@"登录" Font:15.0];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.backgroundColor = ThemeColor;
    [loginBtn.layer setMasksToBounds:YES];
    [loginBtn.layer setCornerRadius:5*displaywidth];
    [loginView addSubview:loginBtn];
    
    regisView = [[UIView alloc] initWithFrame:CGRectMake(0, 430*displaywidth, SCREEN_WIDTH, 200*displaywidth)];
    regisView.backgroundColor = [UIColor clearColor];
    [backImgView addSubview:regisView];
    
    UIImageView *imgView1 = [ZCControl createImageViewWithFrame:CGRectMake(35*displaywidth, 15*displaywidth, 30*displaywidth, 30*displaywidth) ImageName:imgAry[1]];
    imgView1.contentMode = UIViewContentModeScaleAspectFit;
    [regisView addSubview:imgView1];
    UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(70*displaywidth, 15*displaywidth, SCREEN_WIDTH-135*displaywidth, 40*displaywidth) placeholder:@"请确认密码" passWord:NO leftImageView:nil rightImageView:nil Font:14.0];
    [textfield setValue:RGB(179, 179, 179) forKeyPath:@"_placeholderLabel.textColor"];
    textfield.textColor = RGB(77, 77, 77);
    textfield.delegate = self;
    textfield.secureTextEntry = YES;
    registpasswField1 = textfield;
    UIButton *seeBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-60*displaywidth, 20*displaywidth, 25*displaywidth, 20*displaywidth) ImageName:@"biyan" Target:self Action:@selector(seePassAction1:) Title:nil Font:12.0];
    seeBtn.backgroundColor = [UIColor clearColor];
    [regisView addSubview:seeBtn];
    [regisView addSubview:textfield];
    UIView *lineViewa = [[UIView alloc] initWithFrame:CGRectMake(35*displaywidth, 60*displaywidth, SCREEN_WIDTH-70*displaywidth, 1*displaywidth)];
    lineViewa.backgroundColor = ThemeColor;
    [regisView addSubview:lineViewa];
    
    UIImageView *imgViewb = [ZCControl createImageViewWithFrame:CGRectMake(35*displaywidth, 75*displaywidth, 30*displaywidth, 30*displaywidth) ImageName:@"yanzhengma"];
    imgViewb.contentMode = UIViewContentModeScaleAspectFit;
    [regisView addSubview:imgViewb];
    UITextField *textfieldB = [ZCControl createTextFieldWithFrame:CGRectMake(70*displaywidth, 75*displaywidth, SCREEN_WIDTH-205*displaywidth, 40*displaywidth) placeholder:@"请输入验证码" passWord:NO leftImageView:nil rightImageView:nil Font:14.0];
    [textfieldB setValue:RGB(179, 179, 179) forKeyPath:@"_placeholderLabel.textColor"];
    textfieldB.textColor = RGB(77, 77, 77);
    textfieldB.delegate = self;
    [regisView addSubview:textfieldB];
    textfieldB.keyboardType = UIKeyboardTypeNumberPad;
    codeField = textfieldB;
    codeBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-125*displaywidth, 75*displaywidth, 90*displaywidth, 35*displaywidth) ImageName:nil Target:self Action:@selector(verifiBtnAction:) Title:@"获取验证码" Font:12.0];
    [codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    codeBtn.backgroundColor = ThemeColor;
    [codeBtn.layer setCornerRadius:5*displaywidth];
    [codeBtn.layer setMasksToBounds:YES];
    codeBtn.layer.borderColor = ThemeColor.CGColor;
    codeBtn.layer.borderWidth = 1*displaywidth;
    [regisView addSubview:codeBtn];
    UIView *lineViewb = [[UIView alloc] initWithFrame:CGRectMake(35*displaywidth, 120*displaywidth, SCREEN_WIDTH-170*displaywidth, 1*displaywidth)];
    lineViewb.backgroundColor = ThemeColor;
    [regisView addSubview:lineViewb];
    
    agreement = [ZCControl createButtonWithFrame:CGRectMake(30*displaywidth, 130*displaywidth, SCREEN_WIDTH-60*displaywidth, 20*displaywidth) ImageName:@"gouxuandaik" Target:self Action:@selector(agreementAction) Title:@"《用户注册协议》" Font:12.0];
    [agreement setTitleColor:RGB(255, 111, 25) forState:UIControlStateNormal];
    [regisView addSubview:agreement];
    
    UIButton *registeredBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth , 170*displaywidth, SCREEN_WIDTH-120*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(registeredBtnAction) Title:@"确定" Font:15.0];
    [registeredBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registeredBtn.backgroundColor = ThemeColor;
    [registeredBtn.layer setMasksToBounds:YES];
    [registeredBtn.layer setCornerRadius:5*displaywidth];
    
    
    [regisView addSubview:registeredBtn];
    regisView.hidden = YES;
    
}

//协议
- (void)agreementAction
{
    [self showTipView];
}

- (void)showTipView {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    singleTap.cancelsTouchesInView = NO;
    [window addGestureRecognizer:singleTap];
    if (!_coverView) {
        _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0;
        [window addSubview:_coverView];
    }
    _coverView.hidden = NO;
    
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:CGRectMake(40, (SCREEN_HEIGHT - 400*displaywidth)/2, SCREEN_WIDTH - 80, 400*displaywidth)];
        _tipView.backgroundColor = [UIColor whiteColor];
        _tipView.alpha = 0;
        _tipView.layer.cornerRadius = 2;
        _tipView.layer.masksToBounds = YES;
        [window addSubview:_tipView];
    }
    _tipView.hidden = NO;
    [UIView transitionWithView:_coverView duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        _coverView.alpha = 0.4;
        _tipView.alpha = 1;
    } completion:nil];
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 16, _tipView.frame.size.width, 30)];
    tipLabel.text = @"《用户注册协议》";
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = [UIFont systemFontOfSize:18];
    tipLabel.textColor = UIColorHexValue(0x4D4D4D);
    [_tipView addSubview:tipLabel];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(18, 56, _tipView.frame.size.width - 36, 250*displaywidth)];
    [webView loadHTMLString:agreementStr baseURL:nil];
    webView.backgroundColor = [UIColor whiteColor];
    [_tipView addSubview:webView];
    
    UILabel *lineLabel = [ZCControl createLabelWithFrame:CGRectMake(0, _tipView.frame.size.height - 50, _tipView.frame.size.width, 1) Font:16 Text:nil Color:nil];
    lineLabel.backgroundColor = RGB(233, 234, 233);
    [_tipView addSubview:lineLabel];
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(25, _tipView.frame.size.height - 40, (_tipView.frame.size.width - 100) / 2, 30)];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
    cancelBtn.layer.cornerRadius = 5;
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.borderColor = ThemeColor.CGColor;
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.backgroundColor = [UIColor whiteColor];
    [cancelBtn setTitle:@"不同意" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(onClickCancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [_tipView addSubview:cancelBtn];
    UIButton *ensureBtn = [[UIButton alloc] initWithFrame:CGRectMake(25*3 + (_tipView.frame.size.width - 100) / 2, _tipView.frame.size.height - 40, (_tipView.frame.size.width - 100) / 2, 30)];
    ensureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    ensureBtn.backgroundColor = ThemeColor;
    ensureBtn.layer.cornerRadius = 5;
    ensureBtn.layer.masksToBounds = YES;
    [ensureBtn setTitle:@"同意" forState:UIControlStateNormal];
    [ensureBtn addTarget:self action:@selector(onClickEnsureBtn) forControlEvents:UIControlEventTouchUpInside];
    [_tipView addSubview:ensureBtn];
}

- (void)onClickCancelBtn {
    isSelect = NO;
    [agreement setImage:[UIImage imageNamed:@"xuankuang(3)"] forState:UIControlStateNormal];
    [self hideTipView];
}

- (void)onClickEnsureBtn {
    isSelect = YES;
    [agreement setImage:[UIImage imageNamed:@"gouxuandaik"] forState:UIControlStateNormal];
    [self hideTipView];
}

-(void)keyboardHide:(UITapGestureRecognizer*)tap {
    [self hideTipView];
}

- (void)hideTipView {
    if (_coverView) {
        [UIView transitionWithView:_coverView duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            _coverView.alpha = 0;
        } completion:nil];
        _coverView.hidden = YES;
    }
    if (_tipView) {
        [UIView transitionWithView:_tipView duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            _tipView.alpha = 0;
        } completion:nil];
        _tipView.hidden = YES;
    }
}

- (void)requestRegistAgreement {
    WEAKSELF;
    //    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestGetRegistAgreeMentParmers:@{@"key":@"employer_user_agreement"} Success:^(id obj)
     {
         //         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             agreementStr = obj[@"data"][@"propertyValue"];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         //         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

//获取验证码
- (void)verifiBtnAction:(UIButton *)btn
{
    if (loginPhoneField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入手机号" addToView:self.view];
        return;
    }
    if (loginPhoneField.text.length != 11)
    {
        [LTools showMBProgressWithText:@"请输入正确的手机号" addToView:self.view];
        return;
    }
    [self requestRegisCode4register];
    
}

//注册验证码
- (void)requestRegisCode4register
{
    [self.view endEditing:YES];
    WEAKSELF;
    NSDictionary *dic = @{@"phone":loginPhoneField.text
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestCodeRegisterParmers:dic Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             [LTools showMBProgressWithText:@"验证码已发送" addToView:weakSelf.view];
             [weakSelf startTimer];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

- (void)startTimer
{
    seconds = 60;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(calculateTime) userInfo:Nil repeats:YES];
    codeBtn.userInteractionEnabled = NO;
}
//计算时间
- (void)calculateTime
{
    NSString *title = [NSString stringWithFormat:@"%dS",seconds];
    codeBtn.titleLabel.text = title;
    [codeBtn setTitle:title forState:UIControlStateNormal];
    
    if (seconds != 0) {
        seconds --;
    }else
    {
        [self renewTimer];
    }
    
}
//计时器归零
- (void)renewTimer
{
    [timer invalidate];//计时器停止
    codeBtn.userInteractionEnabled = YES;
    [codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    seconds = 60;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (_clickType == 0)
    {
        backImgView.frame = CGRectMake(0, -120*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    }
    else
    {
        backImgView.frame = CGRectMake(0, -190*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    }
    
    
}

- (void)scrollTap:(id)sender
{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
    [self.view endEditing:YES];
}

- (void)jianpanhuishou:(NSNotification *)notification
{
    backImgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}


- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//切换类型 登录
- (void)loginAction
{
    [self.view endEditing:YES];
    _clickType = 0;
    loginView.hidden = NO;
    regisView.hidden = YES;
    theLineView.frame = CGRectMake(0, 300*displaywidth, SCREEN_WIDTH/2, 2*displaywidth);
}

//切换类型 注册
- (void)regisAction
{
    [self.view endEditing:YES];
    _clickType = 1;
    regisView.hidden = NO;
    loginView.hidden = YES;
    theLineView.frame = CGRectMake(SCREEN_WIDTH/2, 300*displaywidth, SCREEN_WIDTH/2, 2*displaywidth);
    
}


//登录
- (void)loginBtnBtnAction
{
    [self.view endEditing:YES];
    if (loginPhoneField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入手机号" addToView:self.view];
        return;
    }
    if (loginpasswField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入密码" addToView:self.view];
        return;
    }
    
    [self requsetPublicKeyParmers];
}

//注册
- (void)registeredBtnAction
{
    if (!isSelect) {
        [LTools showMBProgressWithText:@"请先勾选协议,才可注册" addToView:self.view];
        return;
    }
    [self.view endEditing:YES];
    if (loginPhoneField.text.length != 11)
    {
        [LTools showMBProgressWithText:@"请输入手机号码" addToView:self.view];
        return;
    }
    if ([loginpasswField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请设置密码" addToView:self.view];
        return;
    }
    if ([registpasswField1.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请确认密码" addToView:self.view];
        return;
    }
    if (![loginpasswField.text isEqualToString:registpasswField1.text])
    {
        [LTools showMBProgressWithText:@"两次密码不一致" addToView:self.view];
        return;
    }
    if ([codeField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请输入验证码" addToView:self.view];
        return;
    }
    [self requsetPublicKeyParmers];
}

- (void)requsetPublicKeyParmers {
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:weakSelf.view animated:YES];
    [HYBaseHandler requestPublicKeyParmers:nil Success:^(id obj) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        if ([obj server_Getcodecheck])
        {
            NSString *token = obj[@"data"];
            NSString *encryptStr = [RSA encryptString:loginpasswField.text publicKey:token];
            if (_clickType == 0) {
                NSDictionary *dic = @{@"phone":loginPhoneField.text,
                                      @"password":encryptStr};
                [self requestLogin:dic];
            } else if (_clickType == 1) {
                NSDictionary *dic = @{@"phone":loginPhoneField.text,
                                      @"password":encryptStr,
                                      @"code":codeField.text
                                      };
                [self requestRegisterParmers:dic];
            }
            
            
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
    }];
}

- (void)requestRegisterParmers:(NSDictionary *)dic
{
    WEAKSELF;
    [LTools cache:@"" ForKey:SERVER_LoginToken];
    [MBProgressHUD showHUDAddedTo:weakSelf.view animated:YES];
    [HYBaseHandler requestRegisterParmers:dic Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSString *token = obj[@"data"][@"token"];
             [LTools storage:YES ForKey:SERVER_IsLogin];
             [LTools cache:token ForKey:SERVER_LoginToken];
             NSString *deviceTokenStr = [LTools cacheForKey:@"deviceToken"];
             if ([LTools storageForKey:SERVER_IsLogin] && deviceTokenStr.length>0) {
                 [self requestMessageInit:deviceTokenStr];
             }
             [LTools showMBProgressWithText:@"注册成功" addToView:sharedDelegate.window];
             [weakSelf backAction];
             
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
         
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

- (void)requestLogin:(NSDictionary *)dic {
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:weakSelf.view animated:YES];
    [HYBaseHandler requestUserLoginParmers:dic Success:^(id obj) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        if ([obj server_Getcodecheck])
        {
            [LTools storage:YES ForKey:SERVER_IsLogin];
            [LTools cache:loginPhoneField.text ForKey:SERVER_LoginPhone];
            [LTools cache:obj[@"data"][@"token"] ForKey:SERVER_LoginToken];
            [LTools cache:obj[@"data"][@"id"] ForKey:SERVER_UserID];
            NSString *deviceTokenStr = [LTools cacheForKey:@"deviceToken"];
            if ([LTools storageForKey:SERVER_IsLogin] && deviceTokenStr.length>0) {
                [self requestMessageInit:deviceTokenStr];
            }
            [LTools showMBProgressWithText:@"登录成功" addToView:sharedDelegate.window];
            [weakSelf backAction];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        
    }];
}

//忘记密码
- (void)FindPassword
{
    [self.view endEditing:YES];
}

//显示密码
- (void)seePassAction:(UIButton *)btn
{
    btn.selected = !btn.selected;
    loginpasswField.secureTextEntry = !btn.selected;
    NSString *imgStr = @"zhengyan";
    if (btn.selected)
    {
        imgStr = @"zhengyan";
    }
    [btn setImage:[UIImage imageNamed:imgStr] forState:UIControlStateNormal];
    
}

- (void)requestMessageInit:(NSString *)deviceToken {
    //    WEAKSELF;
    //    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestMessageInitParmers:@{@"deviceToken":deviceToken,@"deviceType":@"2"} Success:^(id obj)
     {
         //         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             //             [LTools showMBProgressWithText:@"提交成功" addToView:weakSelf.view];
         }
         else
         {
             //             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         //         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

- (void)seePassAction1:(UIButton *)btn
{
    btn.selected = !btn.selected;
    registpasswField1.secureTextEntry = !btn.selected;
    NSString *imgStr = @"biyan";
    if (btn.selected)
    {
        imgStr = @"zhengyan";
    }
    [btn setImage:[UIImage imageNamed:imgStr] forState:UIControlStateNormal];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

